package cn.tradewin.pmp.components;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.Block;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Session;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

/**
 * Layout component for pages of application TwdPM.
 */
@Import(stylesheet = {"css/font-awesome.css","css/theme.css"})
public class Layout
{
    /**
     * The page title, for the <title> element and the <h1> element.
     */
    @Property
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String title;

    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block main;
    
    @Property
    private String pageName;
    
    @Inject
    private ComponentResources resources;
    
    @Environmental
    JavaScriptSupport jss;
    
    @Inject
	Messages messages;
    
    @Inject
    Request request;
    
    @AfterRender
    void afterRender() {
    	jss.require("jquery");
    	jss.require("bootstrap");
    }

    public String[] getPageNames() {
    	return new String[] {"master/CustomerManage", "system/guide", "security/AddContractMain"};
    }
    
    public String getNaviCss() {
		 return resources.getPageName().equalsIgnoreCase(pageName) ? "active" : null;
	}
	
	public String getNaviLabel() {
		return messages.get(pageName);
	}
	
	public void onLogout() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			subject.logout();
		}
		resources.discardPersistentFieldChanges();
		Session session = request.getSession(false);
		if (session != null) {
			session.invalidate();
		}
	}
}